package love.yuanmengrobot.qdkaoqin.business;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.util.TeacherConstants;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class EndAttendanceActivity extends AppCompatActivity {
    private Button endtbutton;
    private String atname;
    private String clazzid;
    private String key;

    private TextView tvNumberText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_number_signin);

        Intent intent = getIntent();
        atname = intent.getStringExtra(TeacherConstants.AT_NAME);
        clazzid = intent.getStringExtra(TeacherConstants.CLAZZ_ID);
        key = intent.getStringExtra(TeacherConstants.AT_KEY);
        System.out.println(atname+"&"+clazzid);
        init();
    }

    public void init() {

        tvNumberText=findViewById(R.id.tv_attendance_number);
        tvNumberText.setText(key);
        endtbutton = findViewById(R.id.bu_end_attendance);
        endtbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                setContentView(R.layout.activity_start_number_signin);
//                结束签到
                post(atname,clazzid);




            }
        });
    }
    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response)
                throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println("67777777776:" + body);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (body.contains("成功")) {
                            final AlertDialog alertDialog1 = new AlertDialog.Builder(EndAttendanceActivity.this)
                                    .setTitle("提示")//标题
                                    .setMessage("结束签到成功！")//内容
                                    .setIcon(R.drawable.ic_baseline_check_24)
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            finish();
//                                            //按下确定键后的事件
//                                            //输入后点击“确定”，开始获取我们要的内容 DialogView就是AlertDialog弹出的Activity
//                                            editText.setText(init.getText().toString());
//                                            //控制光标显示位置
//                                            editText.setSelection(editText.getText().length());
                                        }
                                    })
//                                    .setNegativeButton("取消",null)
                                    .create();
                            alertDialog1.show();
//                            final Timer t=new Timer();//设置警告窗口自动关闭
//                            t.schedule(new TimerTask() {
//                                @Override
//                                public void run() {
//                                    alertDialog1.dismiss();
//                                    t.cancel();
//                                }
//                            },2000);//2秒后警告窗口自动关闭


                        } else {
                            final AlertDialog alertDialog1 = new AlertDialog.Builder(EndAttendanceActivity.this)
                                    .setTitle("提示")//标题
                                    .setMessage("结束签到失败！")//内容
                                    .setIcon(R.mipmap.ic_warning)
                                    .create();
                            alertDialog1.show();
                        }
//                        Gson gson = new Gson();
//                        Type jsonType =////////****
//                                new TypeToken<List<Clazz>>() {
//                                }.getType();
//                        List<Clazz> clazzListResponse =
//                                gson.fromJson(body, jsonType);
//                        for (Clazz cl : clazzListResponse) {
//                            clazzAdapter.add(cl);
//                        }
//                        clazzAdapter.notifyDataSetChanged();
                    }
                });
            } else {
            }
        }
    };

    private void post(String atname, String clazzid) {
        FormBody.Builder builder = new FormBody.Builder();
        /* 添加5个参数 */
        builder.add("op", "end")
                .add("atname", atname)
                .add("clazzid", clazzid);
//                .add("memberid", memberid)
//                .add("key", key);
        FormBody body = builder.build();
        Request request = new Request.Builder().url("http://www.yuanmengrobot.love/attendance/Attendance").post(body).build();

        /* 下边和get一样了 */
        try {
            OkHttpClient client = new OkHttpClient();
            client.newCall(request).enqueue(callback);
        } catch (NetworkOnMainThreadException ex) {
            ex.printStackTrace();
        }
    }


}
